package com.atguigu.jxc.controller;

import com.atguigu.jxc.domain.ServiceVO;
import com.atguigu.jxc.domain.SuccessCode;
import com.atguigu.jxc.entity.GoodsType;
import com.atguigu.jxc.service.GoodsTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

/**
 * @author: lei
 * @Date 2021/11/25 21:01
 **/
@RestController
public class GoodsTypeController {

    @Autowired
    private GoodsTypeService goodsTypeService;

    /**
     * 请求URL：http://localhost:8080/goodsType/loadGoodsType
     * 请求参数：无
     * 请求方式：POST
     * 返回值类型：JSON
     * 返回值：String(三层分类id)
     *
     */
    @PostMapping("goodsType/loadGoodsType")
    public String loadGoodsType(){
        return goodsTypeService.loadGoodsType(-1).toString();
    }

    /**
     * 请求URL：http://localhost:8080/goodsType/save
     * 请求参数：String  goodsTypeName,Integer  pId
     * 请求方式：POST
     * 返回值类型：JSON
     * 返回值：ServiceVO
     * Response Example Value：
     * {
     *     "code": 100,
     *     "msg": "请求成功",
     *     "info": null
     * }
     */
    @PostMapping("goodsType/save")
    public ServiceVO saveGoodsType(HttpServletRequest request){
        String goodsTypeName = request.getParameter("goodsTypeName");
        String pId1 = request.getParameter("pId");
        Integer pId = Integer.valueOf(pId1);

        goodsTypeService.save(goodsTypeName,pId);

        return new ServiceVO(SuccessCode.SUCCESS_CODE,SuccessCode.SUCCESS_MESS,null);
    }

    /**
     * 请求URL：http://localhost:8080/goodsType/delete
        TODO 前端无法选中，无法测试，postman要登录
     */
    @PostMapping("goodsType/delete")
    public ServiceVO delete(HttpServletRequest request){
        Integer goodsTypeId =Integer.valueOf(request.getParameter("goodsTypeId")) ;
        goodsTypeService.deleteById(goodsTypeId);

        return new ServiceVO(SuccessCode.SUCCESS_CODE,SuccessCode.SUCCESS_MESS,null);
    }
}
